package com.fpost.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.fpost.manager.ArticleRatingManager;
import com.fpost.manager.IpManager;

public class Article implements Serializable {

	private static final long serialVersionUID = 5145176891338287803L;

	private long articleId;
	private User user;
	private long hitCount;
	private boolean deleted;
	private boolean inDictionary;
	private Date createdOn;
	private long lockSeqNum;
	
	private List<ArticleVersion> versions = new ArrayList<ArticleVersion>();
		
	public Article() {
		super();
	}
	
	public Article(User user) {
		super();
		this.user = user;
	}
	
	public void incrementHitCount() {
		this.hitCount++;
	} 
	
	@Deprecated
	public ArticleVersion getLatestVersion() {
		
		class DateComparator implements Comparator<ArticleVersion> {
			public int compare(ArticleVersion ver1, ArticleVersion ver2) {
				return ver2.getCreatedOn().compareTo(ver1.getCreatedOn());
			}
		}		
		List<ArticleVersion> versionList = new ArrayList<ArticleVersion>(versions);
		Collections.sort(versionList, new DateComparator());
		
		return versionList.get(0);
	}
	
	public long getArticleId() {
		return articleId;
	}
	public void setArticleId(long articleId) {
		this.articleId = articleId;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public long getLockSeqNum() {
		return lockSeqNum;
	}

	public void setLockSeqNum(long lockSeqNum) {
		this.lockSeqNum = lockSeqNum;
	}

	public long getHitCount() {
		return hitCount;
	}

	public void setHitCount(long hitCount) {
		this.hitCount = hitCount;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	public List<ArticleVersion> getVersions() {
		return versions;
	}

	public void setVersions(List<ArticleVersion> versions) {
		this.versions = versions;
	}

	public boolean isInDictionary() {
		return inDictionary;
	}

	public void setInDictionary(boolean inDictionary) {
		this.inDictionary = inDictionary;
	}
	
	public Date getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	public ArticleRatingSummary calculateArticleRating() {
		double temp = 0.0d;
		List<ArticleRating> articleRatings = new ArticleRatingManager().getRatingsByArticleId(this.articleId);
		for (ArticleRating articleRating : articleRatings) {
			temp += articleRating.getRating();
		}
		// if article not rated, averageRating = -1.0d;    
		double averageRating = (articleRatings.size() == 0 ? -1.0d : temp/articleRatings.size()); 
		return new ArticleRatingSummary(averageRating, articleRatings.size());
		
	}

	public void printDetails() {
		System.out.println("Article details:");
		System.out.println("-articleId=" + getArticleId());
		System.out.println("-userId=" + getUser().getUserId());
		System.out.println("-username=" + getUser().getUsername());
		System.out.println("-hitCount=" + getHitCount());
		System.out.println("-createdOn=" + getCreatedOn()); 
		for (ArticleVersion version : versions) {
			version.printDetails();
		}
		List<Ip> ips = new IpManager().getIpsByArticleId(articleId);
		for (Ip ip : ips) {
			ip.printDetails();
		}
	}
	

}
